import { createStore } from "vuex";

const store = createStore({
	state() {
		return {
			// 假设这里存储着当前用户的权限
			userPermissions: ["admin"], // 这里应该动态设置为当前登录用户的权限
			userInfo: JSON.parse(localStorage.getItem("user")),
			// 定义所有可能的导航项
			navigation: [
				{
					name: "文章管理",
					path: "/admin/article-management",
					permissions: ["admin"],
				},
				{
					name: "用户管理",
					path: "/admin/user-management",
					permissions: ["admin"],
				},
				{
					name: "每日一句管理",
					path: "/admin/dailySentence",
					permissions: ["admin"],
				},
				{
					name: "拓展阅读类型管理",
					path: "/admin/extendArticleType",
					permissions: ["admin"],
				},
				{
					name: "拓展阅读管理",
					path: "/admin/extendArticle",
					permissions: ["admin"],
				},
				{
					name: "词源类型管理",
					path: "/admin/etymologyType",
					permissions: ["admin"],
				},
				{
					name: "词源管理",
					path: "/admin/etymology",
					permissions: ["admin"],
				},
				{
					name: "单词管理",
					path: "/admin/enWords",
					permissions: ["admin"],
				},
				{
					name: "轮播图管理",
					path: "/admin/banner",
					permissions: ["admin"],
				},
				{
					name: "商品类型管理",
					path: "/admin/typeList",
					permissions: ["admin"],
				},
				// {
				// 	name: "商品分组专题",
				// 	path: "/admin/shopList",
				// 	permissions: ["admin"],
				// },
				{
					name: "商品管理",
					path: "/admin/ProductList",
					permissions: ["admin"],
				},
				{
					name: "订单管理",
					path: "/admin/OrderList",
					permissions: ["admin"],
				},
				{
					path: "/login",
					name: "退出",
					permissions: ["admin"],
				},
			],
		};
	},
	getters: {
		// 根据用户的权限来过滤导航项
		accessibleNavigation(state) {
			return state.navigation.filter(navItem => {
				// 如果导航项没有指定权限，则它是公共的，否则需要检查用户权限
				return (
					navItem.permissions.length === 0 ||
					navItem.permissions.some(permission =>
						state.userPermissions.includes(permission)
					)
				);
			});
		},
	},
	mutations: {
		changeUserPermissions(state, payload) {
			state.userPermissions = [payload];
		},
		setUser(state, payload) {
			localStorage.setItem("user", JSON.stringify(payload));
			state.userInfo = payload;
		},
	},
	// ...可能还需要一些 mutations 和 actions 来更新用户权限等...
});

export default store;
